package root.utils;

import java.security.MessageDigest;

/**
 * @author 吕梁山
 * @date 2018-09-20
 */
public class StringHelper {

	public static boolean isNotEmpty(String string) {
		return (string != null) && (string.length() > 0);
	}

	/**
	 * 对字符串md5加密
	 */
	public String getMD5(String str) throws Exception {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}
		char[] charArray = str.toCharArray();
		byte[] byteArray = new byte[charArray.length];
		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	public static boolean isEmpty(String str) {
		if ((str == null) || (str.trim().length() == 0)) {
			return true;
		}
		return false;
	}

	public static boolean isEmpty(String... str) {
		for (String s : str) {
			if ((s == null) || (s.trim().length() == 0)) {
				return true;
			}
		}
		return false;
	}
}
